package com.hs.sfzy.controller;

import com.hs.sfzy.entity.CourseCollect;
import com.hs.sfzy.service.CourseCollectService;
import com.hs.sfzy.until.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName CourseCollectController
 * @Description 课程收藏控制器
 * @Author Chen
 * @Date 2025年08月12日 21:56
 * @Version 1.0
 */
@RestController
@RequestMapping("/courseCollect")
public class CourseCollectController {
    @Autowired
    private CourseCollectService courseCollectService;

    /**
     * 根据用户ID查询所有收藏课程
     */
    @GetMapping("{userId}")
    public R getCourseCollectByUserId(@PathVariable("userId") Integer userId) {
        try {
            List<CourseCollect> courseCollects = courseCollectService.getCourseCollectByUserId(userId);
            return R.ok(courseCollects);
        } catch (IllegalArgumentException e) {
            return R.fail(e.getMessage());
        } catch (Exception e) {
            return R.fail("查询收藏课程失败：" + e.getMessage());
        }
    }

    /**
     * 按支付状态查询收藏课程（0=未支付，1=已支付）
     */
    @GetMapping("/listByStatus/{userId}/{payStatus}")
    public R getCourseCollectByStatus(
            @PathVariable("userId") Integer userId,
            @PathVariable("payStatus") Integer payStatus) {
        try {
            List<CourseCollect> courseCollects = courseCollectService.selectWithRelations(userId, payStatus);
            return R.ok(courseCollects);
        } catch (IllegalArgumentException e) {
            return R.fail(e.getMessage());
        } catch (Exception e) {
            return R.fail("查询课程失败：" + e.getMessage());
        }
    }

    /**
     * 添加课程收藏（完整实体参数）
     */
    @PostMapping("collect")
    public R addCourseCollect(@RequestBody CourseCollect courseCollect) {
        try {
            Integer count = courseCollectService.addCourseCollect(courseCollect);
            return count > 0 ? R.ok("收藏成功") : R.fail("收藏失败");
        } catch (RuntimeException e) {
            return R.fail(e.getMessage());
        } catch (Exception e) {
            return R.fail("收藏课程异常：" + e.getMessage());
        }
    }

    /**
     * 通过用户ID和课程ID添加收藏
     */
    @PostMapping("add")
    public R addCourseCollectByUidAndCid(
            @RequestParam("userId") Integer userId,
            @RequestParam("courseId") Integer courseId) {
        try {
            Integer count = courseCollectService.addCourseCollectByUidAndCid(userId, courseId);
            return count > 0 ? R.ok("添加到收藏成功") : R.fail("添加收藏失败");
        } catch (RuntimeException e) {
            return R.fail(e.getMessage());
        } catch (Exception e) {
            return R.fail("添加收藏异常：" + e.getMessage());
        }
    }

    /**
     * 更新支付状态（未支付→已支付）
     */
    @PutMapping("updatePayStatus")
    public R updatePayStatus(
            @RequestParam("userId") Integer userId,
            @RequestParam("courseId") Integer courseId,
            @RequestParam("isok") Integer isok) {
        try {
            int count = courseCollectService.updatePayStatus(userId, courseId, isok);
            return count > 0 ? R.ok("支付状态更新成功") : R.fail("更新失败，未找到对应记录");
        } catch (IllegalArgumentException e) {
            return R.fail(e.getMessage());
        } catch (Exception e) {
            return R.fail("更新支付状态异常：" + e.getMessage());
        }
    }

    /**
     * 取消收藏（删除记录）
     */
    @DeleteMapping("delete")
    public R deleteCourseCollect(
            @RequestParam("userId") Integer userId,
            @RequestParam("courseId") Integer courseId) {
        try {
            int count = courseCollectService.delete(userId, courseId);
            return count > 0 ? R.ok("取消收藏成功") : R.fail("取消收藏失败，未找到对应记录");
        } catch (Exception e) {
            return R.fail("取消收藏异常：" + e.getMessage());
        }
    }

    /**
     * 检查是否已收藏
     */
    @GetMapping("check")
    public R checkIsCollected(
            @RequestParam("userId") Integer userId,
            @RequestParam("courseId") Integer courseId) {

        CourseCollect courseCollect = courseCollectService.checkIsCollected(userId, courseId);
    return courseCollect != null ? R.ok(courseCollect) : R.fail("未找到对应记录");
    }
}
